/*
 * @Author: szx
 * @Date: 2022-01-28 14:22:56
 * @LastEditTime: 2022-01-28 14:36:04
 * @Description:
 * @FilePath: \leetcode\400-499\496\496.js
 */
/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number[]}
 */
var nextGreaterElement = function (nums1, nums2) {
    const res = new Array(nums1.length).fill(-1);
    const map = new Map();
    const st = [];
    for (let i = 0; i < nums2.length; i++) {
        while (st.length && nums2[i] > nums2[st[st.length - 1]]) {
            const index = st.pop();
            // key=数字，value=右侧第一个大的数字
            map.set(nums2[index], nums2[i]);
        }
        st.push(i);
    }
    for (let i = 0; i < nums1.length; i++) {
        res[i] = map.get(nums1[i]) || -1;
    }
    return res;
};
